﻿using Headspring.Training.Domain.Services;
using StructureMap;

namespace Headspring.Training.Domain.Model
{
	[Pluggable(Keys.DEFAULT)]
	public class NotificationFactory : INotificationFactory
	{
		public WorkOrderNotification CreateNotification(IStateCommand commandThatJustHappened, WorkOrder workOrder)
		{
			WorkOrderNotification workOrderNotification = new WorkOrderNotification();
			workOrderNotification.PreviousStatus = commandThatJustHappened.GetBeginStatus();
			workOrderNotification.WorkOrder = workOrder;

			if ((commandThatJustHappened.GetBeginStatus() == WorkOrderStatus.InProgress) &&
			    (workOrder.Status == WorkOrderStatus.Complete))
			{
				//exist
				workOrderNotification.Recipient = workOrder.Creator;
				workOrderNotification.From = workOrder.Assignee;
			}
			else if ((commandThatJustHappened.GetBeginStatus() == WorkOrderStatus.Complete) &&
			         (workOrder.Status == WorkOrderStatus.Assigned))
			{
				//exist
				workOrderNotification.Recipient = workOrder.Assignee;
				workOrderNotification.From = workOrder.Creator;
			}
			else if ((commandThatJustHappened.GetBeginStatus() == WorkOrderStatus.InProgress) &&
			         (workOrder.Status == WorkOrderStatus.Cancelled))
			{
				//exist
				workOrderNotification.Recipient = workOrder.Assignee;
				workOrderNotification.From = workOrder.Creator;
			}
			else
			{
				workOrderNotification = null;
			}

			return workOrderNotification;
		}
	}
}